{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1.7 矢量运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.矢量和标量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:15.994271Z",
     "end_time": "2024-05-07T20:23:16.123958Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 8.66025404, 17.        ])"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "\n",
    "f1 = np.array([10 * math.cos(math.pi / 6), 10 * math.sin(math.pi / 6)])\n",
    "f2 = np.array([0, 12])\n",
    "f = f1 + f2\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "19.078784028338912"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.sum(f ** 2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.123958Z",
     "end_time": "2024-05-07T20:23:16.149532Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "19.078784028338912"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.norm(f)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.149532Z",
     "end_time": "2024-05-07T20:23:16.181461Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.标量积"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "51.96152422706632"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = np.array([6 * math.cos(math.pi / 6), 6 * math.sin(math.pi / 6)])\n",
    "s = np.array([10, 0])\n",
    "np.dot(f, s)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.165705Z",
     "end_time": "2024-05-07T20:23:16.223923Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[19, 22],\n       [43, 50]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[1, 2], [3, 4]])\n",
    "d = np.array([[5, 6], [7, 8]])\n",
    "np.dot(c, d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.181461Z",
     "end_time": "2024-05-07T20:23:16.229536Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "51.96152422706632"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(f, s)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.206752Z",
     "end_time": "2024-05-07T20:23:16.229536Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[17, 23],\n       [39, 53]])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(c, d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.228602Z",
     "end_time": "2024-05-07T20:23:16.270155Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.矢量积"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "3.5"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ab = np.array([4, 1])\n",
    "ac = np.array([1, 2])\n",
    "s = np.cross(ab, ac) / 2\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.249630Z",
     "end_time": "2024-05-07T20:23:16.271253Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-10,  20, -10])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = np.array([1, 2, 3])\n",
    "b = np.array([9, 8, 7])\n",
    "np.cross(v, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.270155Z",
     "end_time": "2024-05-07T20:23:16.291184Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-4, -4])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cross(c, d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.292224Z",
     "end_time": "2024-05-07T20:23:16.348390Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.313325Z",
     "end_time": "2024-05-07T20:23:16.349383Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "array([9, 8, 7])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.328665Z",
     "end_time": "2024-05-07T20:23:16.349383Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 9,  8,  7],\n       [18, 16, 14],\n       [27, 24, 21]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.outer(v, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.349383Z",
     "end_time": "2024-05-07T20:23:16.366146Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.张量积"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15, 18, 21],\n       [42, 54, 66]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(6).reshape(2, 3)\n",
    "b = np.arange(9).reshape(3, 3)\n",
    "np.dot(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.370900Z",
     "end_time": "2024-05-07T20:23:16.412863Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15, 18, 21],\n       [42, 54, 66]])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(a, b, axes=([1], [0]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.391418Z",
     "end_time": "2024-05-07T20:23:16.412863Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "(6, 5, 3, 4)"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(2, size=(2, 6, 5))\n",
    "b = np.random.randint(2, size=(3, 2, 4))\n",
    "np.tensordot(a, b, axes=([0], [1])).shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.398421Z",
     "end_time": "2024-05-07T20:23:16.412863Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 2, 0, 1],\n       [3, 2, 2, 0],\n       [0, 0, 1, 0],\n       [5, 3, 1, 2],\n       [1, 1, 2, 0]])"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.random.randint(2, size=(2, 3, 5))\n",
    "d = np.random.randint(2, size=(3, 2, 4))\n",
    "np.tensordot(c, d, axes=((0, 1), (1, 0)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.412863Z",
     "end_time": "2024-05-07T20:23:16.460773Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "(5, 4)"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(c, d, axes=((0, 1), (1, 0))).shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.428543Z",
     "end_time": "2024-05-07T20:23:16.482546Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-07T20:23:16.444933Z",
     "end_time": "2024-05-07T20:23:16.482546Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
